package com.example.demo.controller;

import com.example.demo.pojo.Student;
import com.example.demo.pojo.Teacher;
import com.example.demo.pojo.User;
import com.example.demo.service.StudentService;
import com.example.demo.service.TeacherService;
import com.example.demo.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.UUID;

@Controller
@Transactional
public class MyController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;
    @ApiOperation(value = "登录页面跳转")
    @RequestMapping("/toSir")
    public String toSir(){
        return "hisir";
    }
    @ApiOperation(value = "登录页面跳转")
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }
    @ApiOperation(value = "用户验证登录")
    @RequestMapping("/login")
    public String login(@RequestParam("uaccount") String account, @RequestParam("upassword")String password,  Model model,HttpSession session1){
        //获取当前用户
        Subject subject= SecurityUtils.getSubject();
        //获取登录令牌
        UsernamePasswordToken token=new UsernamePasswordToken(account,password);
        try {
            String url="";
            //登录跳转shiro过滤处理
            subject.login(token);
          /*  if(subject.hasRole("amdin")){
                return "admin/admin";
            }*/
            //判断用户角色，进行制定跳转
            Session session=subject.getSession();
            User user= (User) session.getAttribute("user");
            if(user.getUrole().equals("admin")){
                session.setAttribute("admin",user);
                url="admin/admin";
            }
            if(user.getUrole().equals("student")){
               Student student=studentService.getStudentByUid(user.getUid());
                session.setAttribute("student",student);
                url="student/student";
            }
            if(user.getUrole().equals("teacher")){
                Teacher teacher=teacherService.getTeacherByUid(user.getUid());
                session.setAttribute("teacher",teacher);
                url="teacher/teacher";
            }
            return url;
        }catch (UnknownAccountException e){
            model.addAttribute("msg","账号错误");
            return "login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码错误");
            return "login";
        }

    }
    @ApiOperation("退出")
    @RequestMapping("/logout")
    public String logout(){
        Subject subject=SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }
    @ApiOperation("跳转注册")
    @RequestMapping("/toRegister")
    public String toRegister(){
        return "register";
    }
    @ApiOperation("用户注册")
    @RequestMapping("/register")
    public String register(@RequestParam("account") String account,
                            @RequestParam("password") String password,@RequestParam("role") String role,
                            HttpSession session){
        String uid= UUID.randomUUID()+account;
        User user=new User(uid,account,password,role);
        try {
            userService.insertUser(user);
            session.setAttribute("user",user);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "student/addStudentInfo";
    }
    @ApiOperation("填写用户个人信息")
    @RequestMapping("/addStudentInfo")
    public String addStudentInfo(@RequestParam("sno") int sno,
                             @RequestParam("sname") String sname,
                             @RequestParam("ssex") String ssex,
                             @RequestParam("sdept") String sdept,
                             @RequestParam("sclass") String sclass,
                             @RequestParam("uid") String uid ,
                             @RequestPart("face") MultipartFile face){
        if(!face.isEmpty()){
            try {
                //获取文件后缀名
                String originalFilename=face.getOriginalFilename();
                String finalFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
                face.transferTo(new File("D://photo//"+finalFileName));
                String path="/Path1/"+finalFileName;
                Student student=new Student(sno,sname,ssex,sdept,sclass,uid,path);
                studentService.insertStudent(student);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return "login";
    }
}
